//
//  CHTitleView.m
//  CarFile
//
//  Created by 陈浩 on 2017/4/11.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHTitleView.h"

@interface CHTitleView ()


@property (nonatomic ,strong) NSArray *titles;
@property (nonatomic ,strong) UIView *indicatorView;
@end

@implementation CHTitleView

-(instancetype)initWithFrame:(CGRect)frame withTitles:(NSArray *)titles{
    
    if (self = [super initWithFrame:frame]) {
        _titles = titles;
        
        [self buildUI];
        
    }
    return self;
    
}

-(void)buildUI{
    
    
    self.indicatorView = [[UIView alloc] init];
    self.indicatorView.backgroundColor = KRGB(252, 61, 106);
    
    for (int i = 0; i < _titles.count; i++) {
        
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(i*(80+20), 0, 80, 40)];
        btn.tag = i+100;
        btn.titleLabel.font = [UIFont systemFontOfSize:18];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btn setTitle:_titles[i] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(titleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        if (i== 0) {
            self.indicatorView.frame = CGRectMake(0, 40, 80, 2);
        }
        
    }
    [self addSubview:self.indicatorView];
    
    
}


-(void)setBgColor:(UIColor *)bgColor{
    _bgColor = bgColor;
    
    self.backgroundColor = bgColor;
    
}

-(void)titleBtnClick:(UIButton *)btn{

    
    [UIView animateWithDuration:0.2 animations:^{
        self.indicatorView.frame = CGRectMake(btn.x, 40, btn.w, 2);
        
    }];
    if ([self.delegate respondsToSelector:@selector(didBtnClickWithTag:)]) {
        [self.delegate didBtnClickWithTag:btn.tag];
    }
    
    
}

@end
